﻿/*******************************************************************************
Copyright (c) 2011-2013, PKStudio

http://pkstudio.org

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*******************************************************************************/

using System;
using System.Windows.Forms;

namespace PKEngine
{
	/// <summary>
	/// Enum for supported keyboard keys.
	/// </summary>
	public enum PKKeybdKey
	{
		Up = Keys.Up,
		Down = Keys.Down,
		Left = Keys.Left,
		Right = Keys.Right,
		Ok = Keys.Enter,
		Cancel = Keys.Escape,
		Next = Keys.PageDown,
		Prev = Keys.PageUp,
		Invalid = 0
	}

	/// <summary>
	/// Keyboard information.
	/// </summary>
	public class PKKeybdEvent
	{
		#region Public Properties
		public PKKeybdKey Key { get; private set; }
		#endregion

		#region Initialization
		public PKKeybdEvent(PKKeybdKey newKey)
		{
			this.Key = newKey;
		}
		#endregion

		#region Public Methods
		/// <summary>
		/// Convert key value to key enum.
		/// </summary>
		/// <param name="keyValue">key value</param>
		/// <returns>key enum</returns>
		public static PKKeybdKey GetKeyEnumFromKeyValue(int keyValue)
		{
			switch (keyValue)
			{
				case (int)PKKeybdKey.Up:
					return PKKeybdKey.Up;

				case (int)PKKeybdKey.Down:
					return PKKeybdKey.Down;

				case (int)PKKeybdKey.Left:
					return PKKeybdKey.Left;

				case (int)PKKeybdKey.Right:
					return PKKeybdKey.Right;

				case (int)PKKeybdKey.Ok:
					return PKKeybdKey.Ok;

				case (int)PKKeybdKey.Cancel:
					return PKKeybdKey.Cancel;

				case (int)PKKeybdKey.Next:
					return PKKeybdKey.Next;

				case (int)PKKeybdKey.Prev:
					return PKKeybdKey.Prev;

				default:
					return PKKeybdKey.Invalid;
			}
		}
		#endregion
	}

	/// <summary>
	/// Interface for KeybdResponder.
	/// </summary>
	public interface IPKKeybdResponder
	{
		bool KeyDown(PKKeybdEvent keybd);
		void KeyUp(PKKeybdEvent keybd);
	}

	/// <summary>
	/// Keybd handler contains the delegate and priority.
	/// </summary>
	public class PKKeybdHandler
	{
		#region Public Properties
		public IPKKeybdResponder Target { get; set; }
		public int Priority { get; set; }
		public bool SwallowesEvents { get; set; }
		public PKKeybdEvent ClaimedEvents { get; set; }
		#endregion

		#region Initialization
		public PKKeybdHandler(IPKKeybdResponder target)
			: this(target, 0, true)
		{
		}

		public PKKeybdHandler(IPKKeybdResponder target, int priority, bool swallowesEvents)
		{
			this.Target = target;
			this.Priority = priority;
			this.SwallowesEvents = swallowesEvents;
		}
		#endregion
	}
}
